﻿using BaseDao;
using BaseSimpleAPI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseSimpleAPI
{
    public class BaseEditModel { public virtual long Id { get; set; } }

    public class BaseViewModel { public virtual long Id { get; set; } }

    /// <summary>
    /// 基础接口
    /// </summary>
    public class BaseController { }

    /// <summary>
    /// 泛型接口
    /// </summary>
    /// <typeparam name="TModel"></typeparam>
    /// <typeparam name="TEditModel"></typeparam>
    /// <typeparam name="TViewModel"></typeparam>
    public class BaseController<TModel, TEditModel, TViewModel> : BaseController
        where TModel : TBaseModel
        where TEditModel : BaseEditModel
        where TViewModel : BaseViewModel
    {
        public virtual BaseService<TModel> Service { get { throw new NotImplementedException(); } }

        public virtual AuthorizationStatus GetAuthorizationStatus(string action, long requestid, long objectid = 0) { throw new NotImplementedException(); }

        /// <summary>
        /// 添加对应的接口
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public virtual StatusData Add(TEditModel model)
        {
            if (GetAuthorizationStatus("Add", NetCallContext.Current.CurrentUserId) != AuthorizationStatus.Accept)
                return StatusData.Error(StatusDataPrompt.GetStatusDataPrompt().NoPermissions);

            string error;
            if (!ModelCheckHelper.CheckModel<TEditModel>(model, out error))
                return StatusData.Error(error);


            // 转换数据模型
            TModel tmodel = TypeHelper.TryChangeType<TModel>(model);

            if (Service.Add(tmodel))
                return StatusObjectData.Success(TypeHelper.TryChangeType<TViewModel>(tmodel));

            return StatusData.Error("Add Error");
        }

        /// <summary>
        /// 根据Id删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual StatusData RemoveById(long id)
        {
            if (GetAuthorizationStatus("RemoveById", NetCallContext.Current.CurrentUserId, id) != AuthorizationStatus.Accept)
                return StatusData.Error(StatusDataPrompt.GetStatusDataPrompt().NoPermissions);

            if (Service.RemoveById(id))
                return StatusData.Success();

            return StatusData.Error(StatusDataPrompt.GetStatusDataPrompt().RemoveError);
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public virtual StatusData Update(TEditModel model)
        {
            if (GetAuthorizationStatus("Update", NetCallContext.Current.CurrentUserId, model.Id) != AuthorizationStatus.Accept)
                return StatusData.Error(StatusDataPrompt.GetStatusDataPrompt().NoPermissions);

            string error;
            if (!ModelCheckHelper.CheckModel<TEditModel>(model, out error))
                return StatusData.Error(error);

            TModel tmodel = TypeHelper.TryChangeType<TModel>(model);

            if (Service.Update(tmodel))
                return StatusObjectData.Success(TypeHelper.TryChangeType<TViewModel>(tmodel));

            return StatusData.Error(StatusDataPrompt.GetStatusDataPrompt().UpdateError);
        }

        /// <summary>
        /// 根据Id获取对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual StatusData GetItemById(long id)
        {
            if (GetAuthorizationStatus("GetItemById", NetCallContext.Current.CurrentUserId, id) != AuthorizationStatus.Accept)
                return StatusData.Error(StatusDataPrompt.GetStatusDataPrompt().NoPermissions);

            TModel model = Service.GetItemById(id);

            if (model != null)
                return StatusObjectData.Success(TypeHelper.TryChangeType<TViewModel>(model));

            return StatusData.Error(StatusDataPrompt.GetStatusDataPrompt().NotFindObjects);
        }

        /// <summary>
        /// 获取全部数据
        /// </summary>
        /// <returns></returns>
        public virtual StatusData GetAll()
        {
            if (GetAuthorizationStatus("GetAll", NetCallContext.Current.CurrentUserId) != AuthorizationStatus.Accept)
                return StatusData.Error(StatusDataPrompt.GetStatusDataPrompt().NoPermissions);

            var all = Service.GetAll();

            if (all != null || all.Count > 0)
                return StatusObjectData.Success(all.Select(n => TypeHelper.TryChangeType<TViewModel>(n)).ToList());
            
            return StatusData.Error(StatusDataPrompt.GetStatusDataPrompt().NotFindObjects);
        }
    }
}
